import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import Tagger.AutoTagAlgorithm;
public class FileExtentionAutoTagAlgorithm implements AutoTagAlgorithm {

	private HashMap<String,String> fromExtensionsToTagsTable;
	public FileExtentionAutoTagAlgorithm(){
		
		fromExtensionsToTagsTable=new HashMap<String,String>();
		
		//search for "FileExtentionAutoTagAlgorithm.txt" file ,to get users definitions for mapping extensions into tags.		
		Path file = Paths.get("FileExtentionAutoTagAlgorithm.txt"); //here the text file should be.
		
	
		if(file.exists())//case the file "FilePathAutoTagAlgorithm.txt" exists, build attributes from it.
		{
			buildFromFile(file);
		}
		else //case the file not exist. create it with default values, and build attributes from it.
		{
			//create it with default values
			
			BufferedWriter out=null;
			try {
				out = new BufferedWriter(new FileWriter(file.toString())); 
				
				//default values.
				String[] data={"txt documents"};
				int size=data.length;
				for(int i=0 ; i<size ; i++){
					out.write(data[i]);
					out.newLine();
				}
			} 
			catch (IOException x) {
			}
			 //close the file
			finally {
			    if (out != null)
					try {
						out.flush();
						out.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}
			
			//build attributes from it.
			buildFromFile(file);
		}
	}
	
	//This method build fromExtensionsToTagsTable attribute, from a file.  
	public void buildFromFile(Path file ){
		InputStream in = null;
		try {
		    in = file.newInputStream();
		    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		    String line = null;
		    //get a line, break it into peaces, and case syntax is good put it in 'fromExtensionsToTagsTable' table.
		    while( (line = reader.readLine()) != null) {
		    	line.toLowerCase();
		    	String[] data=line.split(" ");
		    	if(data.length!=2){//case wrong syntax - continue to next row.
		    		System.out.println("worng syntax on FileExtensionAutoTagAlgorithm.txt file !");
		    		continue;
		    	}
		    	fromExtensionsToTagsTable.put(data[0],data[1]);	
		    }
		    
		} 
		catch (IOException x) {
		}
		 //close the file
		finally {
		    if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	public String getInfo() {
			return "This Algorithm returns tags\n accordimg to key words \nin file extention\n. you can set the mapping \nfrom key words to tags your \nself, by changing the \n'FileExtentionAutoTagAlgorithm.txt' file. \nenjoy.";
	}

	public String[] getTagsForFile(File file) {
		//turn the file to path.
		Path p = file.toPath().toAbsolutePath();
		//get file name. /like "a.txt".
		String fileName=p.getName(p.getNameCount()-1).toString();
		//get the extension.
		int i=fileName.lastIndexOf(".");
		String fileExtension=fileName.substring(i+1,fileName.length());
		fileExtension.toLowerCase();
    	//get the tag from the table for that extension.		
		String[] result= {fromExtensionsToTagsTable.get(fileExtension)};
		return  result;	
	}

}


